package com.doyle.musicplus.client;

import com.doyle.musicplus.shared.SimpleCallback;
import com.google.appengine.api.utils.SystemProperty.Environment;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class LoginFormPresenter extends Presenter {
	
	static String PLACE = "login";

	public LoginFormPresenter(final String someParams, 
			final PresenterDisplay loginDisplay,
			final Environment environment,
			final SimpleCallback<String> callback) {
		
		
		super(someParams, loginDisplay, environment);
		
		SimpleCallback<String> loginSuccessCallback = callback;
		final Object loginService = LoginFormPresenter.this.getEnvironment().getModel().getRemoteLoginService();
		
		loginDisplay.setName("federico");
		loginDisplay.setLoginCallback(new SimpleCallback<Object>() {
			public void goBack(final Object result) {
				
				String name = ((PresenterDisplay) LoginFormPresenter.this.getDisplay()).getName();
				String password = ((PresenterDisplay) LoginFormPresenter.this.getDisplay()).getPasword();
				LoginFormPresenter.this.getDisplay().enableLoginButton(false);
				
				loginService.getSomething(name, password,
						new AsyncCallback<String>() {

							@Override
							public void onFailure(Throwable caught) {
								LoginFormPresenter.this.getEnvironment().showAlert("Falided login");
								LoginFormPresenter.this.getDisplay().enableLoginButton(true);
								loginSuccessCallback.onFailure(new Throwable());
								
							}

							@Override
							public void onSuccess(String result) {
								// TODO Auto-generated method stub
								
							}
					
					
				});
			}
			
			public void enableLoginButton(boolean b) {
				loginButton.setEnabled(b);
			}
			
			
		});
		
	}
	
	

}
